package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo5 {
    public static void main(String[] args) {
        Person p = new Person();
        p.sayHello();

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名：");
        String className = scanner.nextLine();
        System.out.println("请输入方法名:");
        String methodName = scanner.nextLine();
//        System.out.println("请输入传入的参数：");
//        String message = scanner.nextLine();
//        String[] data = message.split(",|，");


        try {
            Class c = Class.forName(className);
            Object obj = c.newInstance();

            //无参
//            Method method = c.getMethod(methodName);//获取无参方法
//            method.invoke(obj);//p.sayHello();

            //有参
//            Method method1 = c.getMethod(methodName, String.class, int.class);
//            method1.invoke(obj, data[0], Integer.parseInt(data[1]));

            //私有
            Method method2 = c.getDeclaredMethod(methodName);
            method2.setAccessible(true);
            method2.invoke(obj);


        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
